package com.tmpt.controller.taglib;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import com.tmpt.service.IMenuService;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tmpt.dto.A2RoleDto;
import com.tmpt.dto.A2UserDesktopDto;
import com.tmpt.dto.A2UserDto;
import com.tmpt.dto.Result;
import com.tmpt.service.ISecurityV2Service;

public class SecurityV2Tag {

	@Resource(name="pageContext") private PageContext pageContext;
	@Resource(name="securityV2Service") private ISecurityV2Service securityV2Service;
	@Resource(name="menuService") private IMenuService menuService;

	/**
	 * 查询菜单json, 包括url等明细，菜单配置用
	 * @return
	 */
	public String menuTree4ConfJson(){
		Result result = securityV2Service.queryMenus4Conf();
		if(!result.isSuccess()){
			return "";
		}
		String menuJson = (String)result.getResultMap().get("menuJson");
		return menuJson;
	}

	/**
	 * 用户菜单JSON
	 * @return
	 */
	public String queryMenusByUser(){
		A2UserDto userDto = (A2UserDto)pageContext.getSession().getAttribute("magSystemDtoSession");
		if(userDto == null){
			return "";
		}
		Result result = securityV2Service.queryMenusByUser(userDto.getId());
		if(!result.isSuccess()){
			return "";
		}
		String menuJson = (String)result.getResultMap().get("menuJson");
		System.out.println(menuJson);
		return menuJson;
	}

	

	/**
	 * 用户桌面图标JSON
	 * @return
	 */
	public String queryDesktopByUser(){
		A2UserDto userDto = (A2UserDto)pageContext.getSession().getAttribute("magSystemDtoSession");
		if(userDto == null){
			return "";
		}
		Result result = securityV2Service.queryDesktopByUser(userDto.getId());
		if(!result.isSuccess()){
			return "";
		}
		List<A2UserDesktopDto> desktopDtoList = result.getResultList();
		addDefaultDesktop(desktopDtoList);
		//desktopDtoList.addAll(0, defaultDesktopList());
		//System.out.println(desktopDtoList);
		String jsonArrayStr = "";
		net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(desktopDtoList); 
		jsonArrayStr = jsonArray.toString();
		//System.out.print("jsonArrayStr:"+jsonArrayStr);
		return jsonArrayStr;
	}
	
	private List<A2UserDesktopDto> addDefaultDesktop(List<A2UserDesktopDto> desktopDtoList){
		A2UserDesktopDto dto = null;
		
		dto = new A2UserDesktopDto();
		dto.setId(-1);
		dto.setText("切换到菜单模式");
		//dto.setHref("http://localhost/zpmng/admin123/desktop/go_menu_mode.jsp");
		dto.setHref("goMenuMode");
		dto.setIcon("images/icon3d/002.png");
		dto.setWidth(250);
		dto.setHeight(250);
		desktopDtoList.add(0,dto);

		dto = new A2UserDesktopDto();
		dto.setId(-1);
		dto.setText("退出系统");
		//dto.setHref("http://localhost/zpmng/admin123/desktop/go_out.jsp");
		dto.setHref("goOut");
		dto.setIcon("images/icon3d/020.png");
		dto.setWidth(250);
		dto.setHeight(250);
		desktopDtoList.add(1,dto);
		
		return desktopDtoList;
	}
	
	/*
	*//**
	 * 角色配置用
	 * @return
	 *//*
	public String menuTree4RoleConfJson(){
		//Result result = securityV2Service.queryMenus4RoleConf();
		Result result = menuService.queryMenus4RoleConf();
		if(!result.isSuccess()){
			return "";
		}
		String menuJson = (String)result.getResultMap().get("menuJson");
		return menuJson;
	}
	*/

	/**
	 * 角色列表
	 * @return
	 */
	public List<A2RoleDto> queryA2RoleList(){

		Result<A2RoleDto> result = securityV2Service.queryA2RoleList();
		if(!result.isSuccess()){
			return new ArrayList<A2RoleDto>();
		}
		List<A2RoleDto> roleDtoList = result.getResultList();

		return roleDtoList;
	}
	
	

	/**
	 * 角色列表 通过UserId查询
	 * @return
	 */
	@Deprecated
	public String queryA2RoleJsonByUser(Map<String, String> paramMap){
		String userId = paramMap.get("userId");
		Result<A2RoleDto> result = securityV2Service.queryA2RoleList();
		Result<A2RoleDto> resultMy = securityV2Service.queryA2RoleListByUser(Long.parseLong(userId));
		if(!result.isSuccess()){
			return "";
		}
		List<A2RoleDto> roleDtoList = result.getResultList();
		
		List<A2RoleDto> roleDtoListMy = resultMy.getResultList();
		System.out.println("-=============:"+roleDtoListMy);

		JSONArray jsonArr = new JSONArray();
		for(A2RoleDto roleDto : roleDtoList){
			JSONObject jo = new JSONObject();
			jo.put("id", roleDto.getId());
			jo.put("text", roleDto.getRoleName());
			jsonArr.put(jo);
		}
		
		
		return jsonArr.toString();
	}
	
}
